package com.mask.ucenter.controller;


import com.alibaba.fastjson.JSON;
import com.mask.roll.common.code.ServiceEnums;
import com.mask.roll.common.constant.RollConstants;
import com.mask.roll.common.ordervo.UcenterMemberOrder;
import com.mask.roll.common.util.JwtUtils;
import com.mask.roll.common.util.RollResponse;
import com.mask.ucenter.entity.Member;
import com.mask.ucenter.entity.VO.RegisterVO;
import com.mask.ucenter.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author hx
 * @since 2021-10-27
 */
@RestController
@CrossOrigin
@RequestMapping("/ucenter/member")
@Api(tags = "用户中心")
@Slf4j
public class MemberController {
    @Autowired
    private MemberService memberService;


    @PostMapping("/login")
    @ApiOperation("用户登录")
    public RollResponse login(@RequestBody Member member){
        if (StringUtils.isEmpty(member.getMobile())){
            return RollResponse.result(ServiceEnums.UCENTER_MEMBER_MOBILE_NULL.getCode(),
                    RollConstants.ERROR,ServiceEnums.UCENTER_MEMBER_MOBILE_NULL.getMessage());
        }
        if (StringUtils.isEmpty(member.getPassword())){
            return RollResponse.result(ServiceEnums.UCENTER_MEMBER_PASSWORD_NULL.getCode(),
                    RollConstants.ERROR,ServiceEnums.UCENTER_MEMBER_PASSWORD_NULL.getMessage());
        }

        return memberService.login(member);
    }

    @PostMapping("/register")
    @ApiOperation("用户注册")
    public RollResponse register(@RequestBody RegisterVO registerVO){
        if (StringUtils.isEmpty(registerVO.getNickname()) ||
                StringUtils.isEmpty(registerVO.getMobile()) ||
                        StringUtils.isEmpty(registerVO.getCode()) ||
                                StringUtils.isEmpty(registerVO.getPassword())) {
            return RollResponse.result(ServiceEnums.UCENTER_MEMBER_REGISTER_NULL.getCode(),
                    RollConstants.ERROR,ServiceEnums.UCENTER_MEMBER_REGISTER_NULL.getMessage());
        }
        return memberService.register(registerVO);
    }

    /**
     * 根据token获取用户信息
     * @param request
     * @return
     */
    @GetMapping("/get/info")
    @ApiOperation("获取用户信息")
    public RollResponse getInfo(HttpServletRequest request){

        //根据request头信息获取id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);

        //根据id查询用户信息
        Member member = memberService.getById(memberId);
        String json = JSON.toJSONString(member);
        log.info("用户id:{}获取信息成功",memberId);
        return RollResponse.result(ServiceEnums.UCENTER_MEMBEER_GET_INFO.getCode(),
                RollConstants.SUCCESS,ServiceEnums.UCENTER_MEMBEER_GET_INFO.getMessage(),json);
    }


    /**
     * 根据用户id查询用户信息
     * @param id
     * @return
     */
    @PostMapping("/get/member/by/{id}")
    public UcenterMemberOrder getInfoById(@PathVariable("id") String id){
        Member member = memberService.getById(id);
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);

        return ucenterMemberOrder;
    }

    /**
     * 查询某一天注册人数
     * @param day
     * @return
     */
    @GetMapping("/count/register/{day}")
    @ApiOperation("查询某一天注册人数")
    public RollResponse getCountRegister(@PathVariable("day") String day){
        Integer count = null;
        try {
            count = memberService.countRegisterDay(day);
            log.info("查询{}这天注册人数为{}",day,count);
            return RollResponse.result(ServiceEnums.STATISTICS_COUNT_REGISTER_DAY_SUCCESS.getCode(),
                    RollConstants.SUCCESS,ServiceEnums.STATISTICS_COUNT_REGISTER_DAY_SUCCESS.getMessage(),count);
        } catch (Exception e) {
            log.error("查询{}这天注册人数失败:{}",day,e.getMessage());
            e.printStackTrace();
            return RollResponse.result(ServiceEnums.STATISTICS_COUNT_REGISTER_DAY_FAILED.getCode(),
                    RollConstants.ERROR,ServiceEnums.STATISTICS_COUNT_REGISTER_DAY_FAILED.getMessage());
        }

    }


}

